<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template v-slot="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hedit(scope.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hdel(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageParams.page"
                :page-sizes="[2, 5, 10, 12]"
                :page-size="pageParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit?'编辑':'新增'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="resetform"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, delRoles, addRoles, getRolesDetail, updateRoles } from '@/api/settings'

export default {
  data() {
    return {
      isEdit: false,
      showDialog: false,
      list: [],
      roleForm: {
        name: '',
        description: ''
      },
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 2 // 每页两条  --- 要与pagination中page-size一致
      },
      total: 0, // 总的数据条数
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述不能为空', trigger: 'blur' }]
      }

    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    resetform() {
      this.$refs.roleForm.resetFields()
    },
    // 更新数据
    async doEdit() {
      try {
        const res = await updateRoles(this.roleForm)
        console.log('updateRoles', res)
        this.loadRoles()
        this.showDialog = false
        // 保存数据
      } catch (err) {
        console.log('updateRoles', err)
      }
    },
    // 请求详细数据
    async loadgetRolesDetail(id) {
      try {
        const res = await getRolesDetail(id)
        console.log('getRolesDetail', res)
        this.roleForm = res.data
        // 保存数据
      } catch (err) {
        console.log('getRolesDetail', err)
      }
    },
    hedit(id) {
      this.showDialog = true
      this.isEdit = true
      this.loadgetRolesDetail(id)
    },

    hd() {
      this.showDialog = true
      this.isEdit = false
    },
    // 点击确定 校验发送请求
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doadd()
      })
    },
    // 新增角色
    async doadd() {
      try {
        const res = await addRoles(this.roleForm)
        console.log('addRole', res)
        if (res.code === 10000) {
          this.$message.success(res.message)
        }
        // 新增数据需要页码跳转到最后一页
        this.total++
        const maxpage = Math.ceil(this.total / this.pageParams.pagesize)
        this.pageParams.page = maxpage
        this.loadRoles()
        this.showDialog = false
        // 保存数据
      } catch (err) {
        console.log('addRole', err)
      }
    },
    hdel(id) {
      this.$confirm('你真的要删除吗？').then(async() => {
        const res = await delRoles(id)
        if (res.code !== 10000) return this.$message.error(res.message)
        console.log(res, '删除')
        this.$message.success(res.message)
        // 当页面的数据只有一条是 点击删除时 当前的页码值需要减一
        if (this.list.length === 1) {
          this.pageParams.page--
          // 页码值最小是一
          if (this.pageParams.page <= 0) {
            this.pageParams.page = 1
          }
        }
        this.loadRoles()
      }).catch(err => err)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageParams.page = 1
      this.pageParams.pagesize = val
      this.loadRoles()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageParams.page = val
      this.loadRoles()
    },
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        console.log(res)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>
